package org.example.dic.conf;

import org.example.annotation.service.IAuthHandleService;
import org.example.annotation.service.IDicHandleService;
import org.example.annotation.service.ILogHandleService;
import org.example.api.psn.IGroupServiceFeignClient;
import org.example.api.psn.IPersonServiceFeignClient;
import org.example.dic.core.service.IDicInfoService;
import org.example.system.common.constant.BaseConstant;
import org.example.system.common.dto.UserAuthDto;
import org.example.system.common.utils.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

/**
 * 自定义注解实现
 */
@Configuration
public class MyAnnotationConf {
    @Lazy
    @Autowired
    IPersonServiceFeignClient personServiceFeignClient;
    @Lazy
    @Autowired
    IGroupServiceFeignClient groupServiceFeignClient;
    @Autowired
    IDicInfoService dicInfoService;

    /**
     * 授权实现
     * @return
     */
    @Bean
    IAuthHandleService getAuthorizeService(){
        return jwt -> {
            ResponseUtil<UserAuthDto> res = personServiceFeignClient.queryAuthInfoByToken();
            return res.getData();
        };
    }

    /**
     * 日志处理实现
     * @return
     */
    @Bean
    ILogHandleService getLogHandleService(){
        return traceLog -> System.out.println("日志处理："+traceLog.toString());
    }

    /**
     * 字典翻译实现
     * @return
     */
    @Bean
    IDicHandleService getDicHandleService(){
        return (type, typeKey, dicKeys) -> {
            if(BaseConstant.DIC_TYPE_PSN.equals(type)){
                //人员名称
                ResponseUtil<String> res = personServiceFeignClient.queryNamesById(dicKeys);
                return res.getData();
            }else if(BaseConstant.DIC_TYPE_GROUP.equals(type)){
                //组织名称
                ResponseUtil<String> res = groupServiceFeignClient.queryNamesById(dicKeys);
                return res.getData();
            }else {
                //数据字典翻译
                return dicInfoService.queryDicTextByKeys(typeKey, dicKeys);
            }
        };
    }
}
